﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginFramework.Common;
using QLBH.Category.Presenter;
using QLBH.StandardUI;
using System.Windows.Forms;
using QLBH.DataModel;
using QLBH.Model.Controller;
using DevComponents.DotNetBar;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    [ExtensionOf(typeof(QLKhoToolExtensionPoint))]
    public class QLKhoTool : BaseView,IQLKhoTool
    {
        public QLKhoTool()
            :base()
        {

        }
        public FormViewBase GetView(object arg)
        {
            return new QLKhoTool();
        }
        public override void AddNew(object sender, EventArgs e)
        {
            AddKhoDlg themKhodlg = new AddKhoDlg();
            if (themKhodlg.ShowDialog() == DialogResult.OK)
            {
                LoadDSKho();
            } 
            base.AddNew(sender, e);
        }
        public override void EditItem(object sender, EventArgs e)
        {
            base.EditItem(sender, e);
            try
            {
                if (DataGridView.SelectedRows.Count > 0)
                {
                    var model = DataGridView.SelectedRows[0].DataBoundItem as IQLKhoModel;
                    AddKhoDlg editKho = new AddKhoDlg();
                    editKho.DlgType = eDlgType.EDIT;
                    editKho.Model = model;
                    if (editKho.ShowDialog() == DialogResult.OK)
                    {
                        LoadDSKho();
                    }
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.InnerException.Message);
            }
        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            base.DeleteItem(sender, e);

            try
            {
                if (DataGridView.SelectedRows.Count > 0)
                {
                    DialogResult result = ShowMessage.Instance.ShowQuestion("Bạn có chắc chắn muốn xóa kho này?");

                    if (result == DialogResult.Yes)
                    {
                        //OK to delete
                        var model = DataGridView.SelectedRows[0].DataBoundItem as IQLKhoModel;
                        KHO entity = model.ToEntity();
                        try
                        {
                            KhoController.GetInstance().Delete(entity);
                        }
                        catch (System.Exception ex)
                        {
                            ShowMessage.Instance.ShowError(ex.Message);
                            return;
                        }
                        LoadDSKho();
                    }
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.InnerException.Message);
            }
        }
        public override void RefreshData(object sender, EventArgs e)
        {
            base.RefreshData(sender, e);

            LoadDSKho();
        }
        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);
            LoadDSKho();
        }
        private void LoadDSKho()
        {
            try
            {
                QLKhoPresenter presen = new QLKhoPresenter();

                DataSource = presen.GetDSKhoHang();

                DataGridView.DataSource = DataSource;

                DataGridView.Columns["KhoID"].Visible = false;
                DataGridView.Columns["KhoID"].DisplayIndex = 5;

                DataGridView.Columns["MaKho"].HeaderText = "Mã Kho";
                DataGridView.Columns["MaKho"].DisplayIndex = 0;

                DataGridView.Columns["TenKho"].HeaderText = "Tên Kho";
                DataGridView.Columns["TenKho"].DisplayIndex = 1;

                DataGridView.Columns["DiaChi"].HeaderText = "Địa Chỉ";
                DataGridView.Columns["DiaChi"].DisplayIndex = 2;

                DataGridView.Columns["GhiChu"].HeaderText = "Ghi Chú";
                DataGridView.Columns["GhiChu"].DisplayIndex = 3;

                DataGridView.Columns["MacDinh"].HeaderText = "Mặc Định";
                DataGridView.Columns["MacDinh"].DisplayIndex = 4;

                if (DataGridView.Rows.Count == 0)
                {
                    ShowMessage.Instance.ShowInfo("Không có kho hàng nào");
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }

        }
        public override void BuildSearchMenu()
        {
            base.BuildSearchMenu();

            ButtonItem btnSearchbyID = new ButtonItem("btnSearchByID", "Tìm theo mã kho");
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo tên kho");

            btnSearchbyID.Click += btnSearchbyID_Click;

            btnSearchByName.Click += btnSearchByName_Click;

            BtnSearch.SubItems.Add(btnSearchbyID);
            BtnSearch.SubItems.Add(btnSearchByName);
        }

        void btnSearchByName_Click(object sender, EventArgs e)
        {
            List<IQLKhoModel> source = (List<IQLKhoModel>)DataSource;

            source = source.Where(m => m.TenKho.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchbyID_Click(object sender, EventArgs e)
        {
            List<IQLKhoModel> source = (List<IQLKhoModel>)DataSource;

            source = source.Where(m => m.MaKho.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLKhoTool
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "QLKhoTool";
            this.Size = new System.Drawing.Size(1312, 560);
            this.ResumeLayout(false);

        }
    }
}
